package com.gitee.minimalismstyle.fresh.flowable.listener

import com.fasterxml.jackson.databind.ObjectMapper
import com.gitee.minimalismstyle.fresh.common.core.domain.R
import com.gitee.minimalismstyle.fresh.common.core.extra.copyProperties
import com.gitee.minimalismstyle.fresh.flowable.api.model.DelegateTaskDto
import org.flowable.common.engine.impl.el.FixedValue
import org.flowable.task.service.delegate.DelegateTask
import org.flowable.task.service.delegate.TaskListener
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component
import org.springframework.web.client.RestTemplate

@Component(value = "taskBusinessCallListener")
class TaskBusinessCallListener: TaskListener {

    @Autowired
    private lateinit var restTemplate: RestTemplate

    @Autowired
    private lateinit var objectMapper: ObjectMapper

    /**
     * url
     */
    private lateinit var  url: FixedValue

    /**
     * 参数 json 字符串
     */
    private lateinit var params: FixedValue

    override fun notify(delegateTask: DelegateTask) {
        val delegateTaskDto = delegateTask.copyProperties(DelegateTaskDto::class)
        val paramMap = objectMapper.readValue(params.expressionText, MutableMap::class.java)
        delegateTaskDto.params = paramMap as MutableMap<String, Any>
        restTemplate.postForObject(url.expressionText, delegateTaskDto, R::class.java )

    }
}